﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Web.UI;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace CRS.Web.BackStageManagement
{
    public partial class RoleList :Page
    {
        private BaseDataManager m_Acc = new BaseDataManager();
        private readonly CastleAccess acc = new CastleAccess();
        private string sql = "select * from ( SELECT Id,Name,Alias,Memo from VitCode_Castle_Role) a";

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (String.IsNullOrEmpty(GenericContext.CurrUserCode))
            {
                Response.Redirect("../Default.aspx");
            }
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            if (!IsPostBack)
            {
                 ListGridInfo.BindGrid();
                
            }

        }


        public object BindList()
        {
            DataTable dt = acc.GetDataTableBySql(sql);
            if(dt != null && dt.Rows.Count > 0)
            {
                btnRealDelete.Enabled = true;
            }
            return dt;
        }
        protected void btnRealDelete_Click(object sender, EventArgs e)
        {
            string strDelVal = ListGridInfo.GetSelectedItem(1);
            if(string.IsNullOrEmpty(strDelVal.Trim()))
            {
                Alert.MessageBox("没有选中角色!");
                return;
            }
            string[] strDel = strDelVal.Split(',');
            int iDeleteCount = 0;
            for (int i = 0; i < strDel.Length; i++)
            {
              if(!string.IsNullOrEmpty(strDel[i]))
              {
                  IList objList = m_Acc.GetObjectList(new UserRoleRelation().GetType().Name, "RoleId", strDel[i]);
                  if(objList != null && objList.Count > 0)
                  {
                      IList RoleList = m_Acc.GetObjectList(new Role().GetType().Name, "Id", strDel[i]);
                      if(RoleList != null && RoleList.Count > 0)
                      {
                          var CurrentRole = (Role) RoleList[0];
                          var strRoleName = CurrentRole.Name;
                          Alert.MessageBox( "有用户属于角色(" + strRoleName + ")，不能删除!");
                          return;
                      }
                   }
              }
               if (m_Acc.DeleteObjById(new Role(), strDel[i]))
                    iDeleteCount++;
                var nameValueCollection = new NameValueCollection {{"ObjClassName", "Role"}, {"ObjId", strDel[i]}};
                var objList2 = m_Acc.GetObjectList(new Permission().GetType().Name, nameValueCollection);
               if (objList2 != null && objList2.Count > 0)
               {
                   for (var j = 0; j < objList2.Count; j++)
                   {
                       var currentPermission = (Permission)objList2[j];
                       m_Acc.DeleteObj(currentPermission);
                   }

               }

            }
            Alert.MessageBox("有" + iDeleteCount + "条记录被成功删除!");
            ListGridInfo.BindGrid();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            const string strLink = "RoleEdit.aspx?Type=Add";
            Response.Redirect(strLink, false);
        }

        protected void ListGridInfo_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            string val = e.SelectedDataKeyValue;
            Response.Redirect("RoleEdit.aspx?Type=Edit&Id=" + val, false);
           
        }

        protected void btnQuery_Click1(object sender, EventArgs e)
        {
            if (ddlCondition.SelectedItem.Text != "" && txtValue.Text.Trim().Length > 0)
            {

                if (rbtAmount.Checked)
                    sql += "  where " + ddlCondition.SelectedItem.Value + " = '" + txtValue.Text.Trim() + "'";
                else sql += "  where " + ddlCondition.SelectedItem.Value + " like '%" + txtValue.Text.Trim() + "%'";


            }
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            ListGridInfo.BindGrid();
        }

        protected void ListGridInfo_OnBeforeExportData(object sender, PaginationGridBeforeExportDataArgs e)
        {
            if (e.ExportMode == PaginationGridDataExportMode.All)
                return;
        }
    }
}
